<?php
@define('__PNADDRESSBOOK__','pnAddressBook');
// $Id: pnuser.php,v 1.4 2003/05/22 22:58:21 garrett Exp $
// ----------------------------------------------------------------------
// POST-NUKE Content Management System
// Copyright (C) 2002 by the PostNuke Development Team.
// http://www.postnuke.com/
// ----------------------------------------------------------------------
// Based on:
// PHP-NUKE Web Portal System - http://phpnuke.org/
// Thatware - http://thatware.org/
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WIthOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Original Author of file: Thomas Smiatek
// Purpose of file:  pnAddressBook administration display functions
// ----------------------------------------------------------------------

//=========================================================================
//  Load the API Functions
//=========================================================================
pnModAPILoad(__PNADDRESSBOOK__,'user');

//=========================================================================
//  the main function
//=========================================================================
function pnAddressBook_user_main() {

    $output = new pnHTML();
	$output->SetInputMode(_PNH_VERBATIMINPUT);

    //Private Address Book mode
	if ((!pnUserLoggedIn()) && (pnModGetVar(__PNADDRESSBOOK__, 'globalprotect')==1)) {
		$output->Text(pnAddressBook_themetable('start'));
		$output->Text(pnVarPrepHTMLDisplay(_pnAB_REGONLY));
        $output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();

	}

	// Security check
    if(!pnModAPIFunc(__PNADDRESSBOOK__,'user','checkAccessLevel',array('option'=>'view'))) {
        $output->Text(pnAddressBook_themetable('start'));
		$output->Text(pnVarPrepHTMLDisplay(_PNADDRESSBOOK_NOAUTH));
        $output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
    }

    //$output->Text(pnAddressBook_user_menu());
	$output->Text(pnAddressBook_user_viewAll());
	$output->Linebreak(1);

    // Return the output that has been generated by this function
    $output->SetInputMode(_PNH_PARSEINPUT);
	return $output->GetOutput();
}

//=========================================================================
//  Show all contacts
//=========================================================================
function pnAddressBook_user_viewAll() {
	global $bgcolor1,$bgcolor2,$bgcolor3,$bgcolor4;

	$total=pnVarCleanFromInput('total');
	$page=pnVarCleanFromInput('page');
	$char=pnVarCleanFromInput('char');

	// Start Page
	$output = new pnHTML();
	$output->SetInputMode(_PNH_VERBATIMINPUT);

	// Security check
	if (!pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_READ)) {
        $output->Text(pnVarPrepHTMLDisplay(_PNADDRESSBOOK_NOAUTH));
        $output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
    }

	// Check Access Level
	if(!pnModAPIFunc(__PNADDRESSBOOK__,'user','checkAccessLevel',array('option'=>'view'))) {
        $output->Text(pnVarPrepHTMLDisplay(_PNADDRESSBOOK_NOAUTH));
        $output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
    }

	// Menu
	$output->Text(pnAddressBook_user_menu());

	// Get the menu values
	$formvalues = pnModAPIFunc(__PNADDRESSBOOK__,'user','getMenuValues');
	extract($formvalues);

	// SQL Query
	list($dbconn) = pnDBGetConn();
    $pntable = pnDBGetTables();
	$address_table = $pntable['pnaddressbook_address'];
    $address_column = &$pntable['pnaddressbook_address_column'];

	// Note Searchorder
	if ($sortview != 1) {
		$sort_1 = pnModGetVar(__PNADDRESSBOOK__, 'sortorder_1');
		$sql = "SELECT *, CONCAT($sort_1) AS listname FROM $address_table";
	}
	else {
		$sort_2 = pnModGetVar(__PNADDRESSBOOK__, 'sortorder_2');
		$sql = "SELECT *, CONCAT($sort_2) AS listname FROM $address_table";
	}

	// Get user id
	if (pnUserLoggedIn()) { $user_id = pnUserGetVar('uid');}
	else {$user_id = 0;}

	// Private Contacts only
	// if globalprotect, only records of the user are shown
	if (((pnModGetVar(__PNADDRESSBOOK__, 'globalprotect'))==1) && (!pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_EDIT))) {
		$sql .= " WHERE ($address_column[user_id]=$user_id)";
	}
	else {
		// if private = 1, show only private records
		if ($menuprivate==1) {
			// Admins always see all records
			if (pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_EDIT)) {
				$sql .= " WHERE ($address_column[user_id]=$user_id)";
			}
			else {
				$sql .= " WHERE ($address_column[user_id]=$user_id AND $address_column[private] = 1)";
			}
		}
		else {
			// Admins always see all records
			if (pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_EDIT)) {
				$sql .= " WHERE ($address_column[user_id] IS NOT NULL)";
			}
			else {
				// if private = 0, show all records
				$sql .= " WHERE (($address_column[private] = 0) OR ($address_column[user_id]=$user_id AND $address_column[private] = 1))";
			}
		}
	}


	// Filter Categories
	if ($catview) {
		$sql .= " AND ($address_column[cat_id] = $catview)";
	}

	// A-Z
	if ($all == 0) {
		if ($sortview != 1) {
			$sortCols = explode(',',pnModGetVar(__PNADDRESSBOOK__, 'sortorder_1'));
		}
		else {
			$sortCols = explode(',',pnModGetVar(__PNADDRESSBOOK__, 'sortorder_2'));
		}
		if ($sortCols[0] == 'adr_sortname') {
			if ($char) { $sql .= " AND ($address_column[sortname] LIKE '$char%')"; }
			else { $sql .= " AND ($address_column[sortname] LIKE 'A%')"; }
		}
		else {
			if ($sortCols[0] == 'adr_sortcompany') {
				if ($char) { $sql .= " AND ($address_column[sortcompany] LIKE '$char%')"; }
				else { $sql .= " AND ($address_column[sortcompany] LIKE 'A%')"; }
			}
			else {
				if ($char) { $sql .= " AND (".$sortCols[0]." LIKE '$char%')"; }
				else { $sql .= " AND (".$sortCols[0]." LIKE 'A%')"; }
			}
		}
	}

	// Search
	if ($formSearch) {
		$sql .= " AND ($address_column[lname] LIKE '%$formSearch%'
				  OR $address_column[fname] LIKE '%$formSearch%'
				  OR $address_column[company] LIKE '%$formSearch%'
				  OR $address_column[title] LIKE '%$formSearch%'
				  OR $address_column[city] LIKE '%$formSearch%'
				  OR $address_column[address1] LIKE '%$formSearch%'
				  OR $address_column[address2] LIKE '%$formSearch%'
				  OR $address_column[zip] LIKE '%$formSearch%'
				  OR $address_column[country] LIKE '%$formSearch%'
				  OR $address_column[state] LIKE '%$formSearch%'
				  OR $address_column[note] LIKE '%$formSearch%'
				  OR $address_column[contact_1] LIKE '%$formSearch%'
				  OR $address_column[contact_2] LIKE '%$formSearch%'
				  OR $address_column[contact_3] LIKE '%$formSearch%'
				  OR $address_column[contact_4] LIKE '%$formSearch%'
				  OR $address_column[contact_5] LIKE '%$formSearch%')";

		$cus_fields = pnModAPIFunc(__PNADDRESSBOOK__,'user','customFieldInformation',array('id'=>''));
		foreach($cus_fields as $cus) {
			if ((!strstr($cus['type'],'tinyint')) && (!strstr($cus['type'],'smallint'))) {
				$the_name = 'adr_custom_'.$cus['nr'];
				if (strstr($cus['type'],'varchar')) {
					$sql .= " OR $the_name LIKE '%$formSearch%'";
				}
			}
		}
	}

	// Sort
	$sql .= " ORDER BY listname ASC";

	if (!$total) {
		$numRec = $dbconn->Execute($sql);
		$total = $numRec->RecordCount();
		$page = 1;
		$numRec->Close();
	}

	$items = pnModGetVar(__PNADDRESSBOOK__, 'itemsperpage');
	$result = $dbconn->PageExecute($sql,$items,$page);
	if ($dbconn->ErrorNo() != 0) {
        //$output->Text(pnVarPrepHTMLDisplay(_PNADDRESSBOOK_NOAUTH));
        $output->Text('ERROR');
		$output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
    }

	//Show Result
	$output->Linebreak(1);
	$output->Text(pnAddressBook_themetable('start'));
	// some design ;)
	$bc1 = $bgcolor1;
	$bc2 = $bgcolor2;
	if ($bgcolor1 == $bgcolor2) {
		if ($bgcolor1 == $bgcolor3) {
			$bc2 = $bgcolor4;
		}
		else {
			$bc2 = $bgcolor3;
		}
	}

	// A-Z Navigation
	$selChar= ((isset($char)) ? $char : '');
	if ($all==0) {
		$output->Text('<div align="center">');
		$numPages = (($total/$items)+1);
		for($i=65;$i<=90;$i++) {
			$char = chr($i);
			$params = array('authid'=>pnSecGenAuthKey(),
							'sortview'=>$sortview,
							'catview'=>$catview,
							'menuprivate'=>$menuprivate,
							'all'=>$all,
							'char'=>$char);

			$pageURL	= pnModURL(__PNADDRESSBOOK__,'user','viewAll',$params);
			if ($i != 65) { $output->Text(' | '); }
			if ($char == $selChar) { $output->Text('<b><u>'.$char.'</u></b>'); }
			else { $output->URL($pageURL,$char); }
		}
		$output->Text('</div>');
		$output->Linebreak(1);
	}

	// No Records found!
	if ($total < 1) {
		$output->Linebreak(1);
		$output->Text('<div align="center">'.pnVarPrepHTMLDisplay(_pnAB_NORECORDS).'</div>');
		$output->Linebreak(1);
		$output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
	}

	$output->Text('<table width="100%" align="center" cellpadding="5" cellspacing="1" bgcolor="'.$bc2.'">');
	$output->TableRowStart();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	if ($sortview != 1) {
		//$output->Text('<b>'.pnVarPrepHTMLDisplay(_pnAB_TABLE_NAME).'</b>');
		$headers = pnModAPIFunc(__PNADDRESSBOOK__,'user','getListHeader',array('sort'=>1));
		$output->Text('<b>'.pnVarPrepHTMLDisplay($headers[0]).'</b>');
	}
	else {
		//$output->Text('<b>'.pnVarPrepHTMLDisplay(_pnAB_TABLE_COMPANY).'</b>');
		$headers = pnModAPIFunc(__PNADDRESSBOOK__,'user','getListHeader',array('sort'=>2));
		$output->Text('<b>'.pnVarPrepHTMLDisplay($headers[0]).'</b>');
	}
	$output->TableColEnd();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	if ($sortview != 1) {
		//$output->Text('<b>'.pnVarPrepHTMLDisplay(_pnAB_TABLE_COMPANY).'</b>');
		$headers = pnModAPIFunc(__PNADDRESSBOOK__,'user','getListHeader',array('sort'=>1));
		$output->Text('<b>'.pnVarPrepHTMLDisplay($headers[1]).'</b>');
	}
	else {
		//$output->Text('<b>'.pnVarPrepHTMLDisplay(_pnAB_TABLE_NAME).'</b>');
		$headers = pnModAPIFunc(__PNADDRESSBOOK__,'user','getListHeader',array('sort'=>2));
		$output->Text('<b>'.pnVarPrepHTMLDisplay($headers[1]).'</b>');
	}
	$output->TableColEnd();
//gehSTART
    // Donor
    $output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text('<b><acronym title="Donor">'.pnVarPrepHTMLDisplay("D").'</acronym></b>');
    $output->TableColEnd();
    // Caretaker
    $output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text('<b><acronym title="Caretaker">'.pnVarPrepHTMLDisplay("C").'</acronym></b>');
    $output->TableColEnd();
    // Member
    $output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text('<b><acronym title="Member">'.pnVarPrepHTMLDisplay("M").'</acronym></b>');
    $output->TableColEnd();
    // TNR
    $output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text('<b><acronym title="Trap, Neuter, Return">'.pnVarPrepHTMLDisplay("T").'</acronym></b>');
    $output->TableColEnd();
    // Volunteer
    $output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text('<b><acronym title="Volunteer">'.pnVarPrepHTMLDisplay("V").'</acronym></b>');
    $output->TableColEnd();
    // Foster
    $output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text('<b><acronym title="Foster">'.pnVarPrepHTMLDisplay("FO").'</acronym></b>');
    $output->TableColEnd();
    // Feeder
    $output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text('<b><acronym title="Feeder">'.pnVarPrepHTMLDisplay("FE").'</acronym></b>');
    $output->TableColEnd();
    // Food Source
    $output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text('<b><acronym title="Food Source">'.pnVarPrepHTMLDisplay("FS").'</acronym></b>');
    $output->TableColEnd();
//gehEND
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->Text('<b>'.pnVarPrepHTMLDisplay(_pnAB_TABLE_CONTACT).'</b>');
	$output->TableColEnd();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->Text('<b>'.pnVarPrepHTMLDisplay(_pnAB_TABLE_ACTION).'</b>');
	$output->TableColEnd();
	$output->TableRowEnd();

	for (; !$result->EOF; $result->MoveNext()) {
        list($adr_id,$adr_cat_id,$adr_prefix,$adr_lname,$adr_fname,$adr_sortname,$adr_title,$adr_company,$adr_sortcompany,$adr_img,$adr_zip,$adr_city,$adr_address1,$adr_address2,$adr_state,$adr_country,$adr_contact_1,$adr_contact_2,$adr_contact_3,$adr_contact_4,$adr_contact_5,$adr_c_label_1,$adr_c_label_2,$adr_c_label_3,$adr_c_label_4,$adr_c_label_5,$adr_c_main,$adr_custom_1,$adr_custom_2,$adr_custom_3,$adr_custom_4,$adr_note,$adr_user,$adr_private,$adr_date,$adr_listname,$adr_custom_5,$adr_custom_6,$adr_custom_7,$adr_custom_8) = $result->fields;

		$cus_fields = pnModAPIFunc(__PNADDRESSBOOK__,'user','customFieldInformation',array('id'=>$adr_id));
		$i=1;
		foreach($cus_fields as $cus) {
			if ($cus['type']=='date default NULL') {
				$cus['value'] = pnModAPIFunc(__PNADDRESSBOOK__,'user','stamp2date',array('idate'=>$cus['value']));
			}
			$the_name = 'adr_custom_'.$i;
			$$the_name = $cus['value'];
			$i++;
		}

		$output->TableRowStart();
		if ($sortview != 1) {
			$sortCols = explode(',',pnModGetVar(__PNADDRESSBOOK__, 'sortorder_1'));
		}
		else {
			$sortCols = explode(',',pnModGetVar(__PNADDRESSBOOK__, 'sortorder_2'));
		}
		$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
		if ($sortCols[0] == 'adr_sortname') {
			if (pnModGetVar(__PNADDRESSBOOK__, 'name_order')==1) {
				if ((!empty($adr_fname)) && (!empty($adr_lname))) { $output->Text(pnVarPrepHTMLDisplay($adr_fname).' '.pnVarPrepHTMLDisplay($adr_lname)); }
				else { $output->Text(pnVarPrepHTMLDisplay($adr_fname).pnVarPrepHTMLDisplay($adr_lname)); }
			}
			else {
				if ((!empty($adr_lname)) && (!empty($adr_fname))) { $output->Text(pnVarPrepHTMLDisplay($adr_lname).', '.pnVarPrepHTMLDisplay($adr_fname)); }
				else { $output->Text(pnVarPrepHTMLDisplay($adr_lname).pnVarPrepHTMLDisplay($adr_fname)); }
			}
		}
		else {
			if ($sortCols[0] == 'adr_sortcompany') { $output->Text(pnVarPrepHTMLDisplay($adr_company)); }
			else {$output->Text(pnVarPrepHTMLDisplay($$sortCols[0])); }
		}
		$output->TableColEnd();

		$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');

		if ($sortCols[1] == 'adr_sortname') {
			if (pnModGetVar(__PNADDRESSBOOK__, 'name_order')==1) {
				if ((!empty($adr_fname)) && (!empty($adr_lname))) { $output->Text(pnVarPrepHTMLDisplay($adr_fname).' '.pnVarPrepHTMLDisplay($adr_lname)); }
				else { $output->Text(pnVarPrepHTMLDisplay($adr_fname).pnVarPrepHTMLDisplay($adr_lname)); }
			}
			else {
				if ((!empty($adr_lname)) && (!empty($adr_fname))) { $output->Text(pnVarPrepHTMLDisplay($adr_lname).', '.pnVarPrepHTMLDisplay($adr_fname)); }
				else { $output->Text(pnVarPrepHTMLDisplay($adr_lname).pnVarPrepHTMLDisplay($adr_fname)); }
			}
		}
		else {
			if ($sortCols[1] == 'adr_sortcompany') { $output->Text(pnVarPrepHTMLDisplay($adr_company)); }
			else {$output->Text(pnVarPrepHTMLDisplay($$sortCols[1])); }
		}

		$output->TableColEnd();
//gehSTART
    // Donor
    $output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
    if (!empty($adr_custom_1)) {
        $output->Text('<acronym title="Is a Donor">'.pnVarPrepHTMLDisplay("D").'</acronym>');
    } else {
        $output->Text(' ');
    }
    $output->TableColEnd();
    // Cartaker
    $output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
    if (!empty($adr_custom_2)) {
        $output->Text('<acronym title="Is a Caretaker">'.pnVarPrepHTMLDisplay("C").'</acronym>');
    } else {
        $output->Text(' ');
    }
    $output->TableColEnd();
    // Member
    $output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
    if (!empty($adr_custom_3)) {
        $output->Text('<acronym title="Is a Member">'.pnVarPrepHTMLDisplay("M").'</acronym>');
    } else {
        $output->Text(' ');
    }
    $output->TableColEnd();
    // TNR
    $output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
    if (!empty($adr_custom_4)) {
        $output->Text('<acronym title="Performs TNR">'.pnVarPrepHTMLDisplay("T").'</acronym>');
    } else {
        $output->Text(' ');
    }
    $output->TableColEnd();
    // Volunteer
    $output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
    if (!empty($adr_custom_6)) {
        $output->Text('<acronym title="Is a Volunteer">'.pnVarPrepHTMLDisplay("V").'</acronym>');
    } else {
        $output->Text(' ');
    }
    $output->TableColEnd();
    // Foster
    $output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
    if (!empty($adr_custom_5)) {
        $output->Text('<acronym title="Is a Foster">'.pnVarPrepHTMLDisplay("FO").'</acronym>');
    } else {
        $output->Text(' ');
    }
    $output->TableColEnd();
    // Feeder
    $output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
    if (!empty($adr_custom_7)) {
        $output->Text('<acronym title="Is a Feeder">'.pnVarPrepHTMLDisplay("FE").'</acronym>');
    } else {
        $output->Text(' ');
    }
    $output->TableColEnd();
    // Food Source
    $output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
    if (!empty($adr_custom_8)) {
        $output->Text('<acronym title="Is a Food Source">'.pnVarPrepHTMLDisplay("FS").'</acronym>');
    } else {
        $output->Text(' ');
    }
    $output->TableColEnd();
//gehEND
		$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
		switch($adr_c_main) {
			case 0:
				if(!pnModAPIFunc(__PNADDRESSBOOK__,'user','is_email',array('email'=>$adr_contact_1))) {
					if(!pnModAPIFunc(__PNADDRESSBOOK__,'user','is_url',array('url'=>$adr_contact_1))) {
						$output->Text(pnVarPrepHTMLDisplay($adr_contact_1));
					}
					else {
						$output->Text('<a href="'.pnVarPrepHTMLDisplay($adr_contact_1).'" target="_blank">'.pnVarPrepHTMLDisplay($adr_contact_1).'</a>');
					}
				}
				else {
					$output->Text('<a href="mailto:'.pnVarPrepHTMLDisplay($adr_contact_1).'">'.pnVarPrepHTMLDisplay($adr_contact_1).'</a>');
				}
				break;
			case 1:
				if(!pnModAPIFunc(__PNADDRESSBOOK__,'user','is_email',array('email'=>$adr_contact_2))) {
					if(!pnModAPIFunc(__PNADDRESSBOOK__,'user','is_url',array('url'=>$adr_contact_2))) {
						$output->Text(pnVarPrepHTMLDisplay($adr_contact_2));
					}
					else {
						$output->Text('<a href="'.pnVarPrepHTMLDisplay($adr_contact_2).'">'.pnVarPrepHTMLDisplay($adr_contact_2).'</a>');
					}
				}
				else {
					$output->Text('<a href="mailto:'.pnVarPrepHTMLDisplay($adr_contact_2).'">'.pnVarPrepHTMLDisplay($adr_contact_2).'</a>');
				}
				break;
			case 2:
				if(!pnModAPIFunc(__PNADDRESSBOOK__,'user','is_email',array('email'=>$adr_contact_3))) {
					if(!pnModAPIFunc(__PNADDRESSBOOK__,'user','is_url',array('url'=>$adr_contact_3))) {
						$output->Text(pnVarPrepHTMLDisplay($adr_contact_3));
					}
					else {
						$output->Text('<a href="'.pnVarPrepHTMLDisplay($adr_contact_3).'">'.pnVarPrepHTMLDisplay($adr_contact_3).'</a>');
					}
				}
				else {
					$output->Text('<a href="mailto:'.pnVarPrepHTMLDisplay($adr_contact_3).'">'.pnVarPrepHTMLDisplay($adr_contact_3).'</a>');
				}
				break;
			case 3:
				if(!pnModAPIFunc(__PNADDRESSBOOK__,'user','is_email',array('email'=>$adr_contact_4))) {
					if(!pnModAPIFunc(__PNADDRESSBOOK__,'user','is_url',array('url'=>$adr_contact_4))) {
						$output->Text(pnVarPrepHTMLDisplay($adr_contact_4));
					}
					else {
						$output->Text('<a href="'.pnVarPrepHTMLDisplay($adr_contact_4).'">'.pnVarPrepHTMLDisplay($adr_contact_4).'</a>');
					}
				}
				else {
					$output->Text('<a href="mailto:'.pnVarPrepHTMLDisplay($adr_contact_4).'">'.pnVarPrepHTMLDisplay($adr_contact_4).'</a>');
				}
				break;
			case 4:
				if(!pnModAPIFunc(__PNADDRESSBOOK__,'user','is_email',array('email'=>$adr_contact_5))) {
					if(!pnModAPIFunc(__PNADDRESSBOOK__,'user','is_url',array('url'=>$adr_contact_5))) {
						$output->Text(pnVarPrepHTMLDisplay($adr_contact_5));
					}
					else {
						$output->Text('<a href="'.pnVarPrepHTMLDisplay($adr_contact_5).'">'.pnVarPrepHTMLDisplay($adr_contact_5).'</a>');
					}
				}
				else {
					$output->Text('<a href="mailto:'.pnVarPrepHTMLDisplay($adr_contact_5).'">'.pnVarPrepHTMLDisplay($adr_contact_5).'</a>');
				}
				break;
			default:
				if(!pnModAPIFunc(__PNADDRESSBOOK__,'user','is_email',array('email'=>$adr_contact_1))) {
					if(!pnModAPIFunc(__PNADDRESSBOOK__,'user','is_url',array('url'=>$adr_contact_1))) {
						$output->Text(pnVarPrepHTMLDisplay($adr_contact_1));
					}
					else {
						$output->Text('<a href="'.pnVarPrepHTMLDisplay($adr_contact_1).'">'.pnVarPrepHTMLDisplay($adr_contact_1).'</a>');
					}
				}
				else {
					$output->Text('<a href="mailto:'.pnVarPrepHTMLDisplay($adr_contact_1).'">'.pnVarPrepHTMLDisplay($adr_contact_1).'</a>');
				}
				break;
		}
		$output->TableColEnd();
		$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
		$detailargs=array('id'=>$adr_id,
						'formcall'=>'edit',
						'authid'=>pnSecGenAuthKey(),
						'catview'=>$catview,
						'sortview'=>$sortview,
						'formSearch'=>urlencode($formSearch),
						'all'=>$all,
						'menuprivate'=>$menuprivate,
						'total'=>$total,
						'page'=>$page,
						'char'=>$selChar);

		$detailURL=pnModURL(__PNADDRESSBOOK__,'user','viewDetail',$detailargs);
		$detailTXT=pnVarPrepHTMLDisplay(_pnAB_TABLE_SHOWDETAIL);
		$deleteURL=pnModURL(__PNADDRESSBOOK__,'user','confirmDelete',$detailargs);
		$deleteTXT=pnVarPrepHTMLDisplay(_pnAB_TABLE_DELETE);
		$editURL=pnModURL(__PNADDRESSBOOK__,'user','insertedit',$detailargs);
		$editTXT=pnVarPrepHTMLDisplay(_pnAB_TABLE_EDIT);
		$output->URL($detailURL,$detailTXT);
		if(pnModAPIFunc(__PNADDRESSBOOK__,'user','checkAccessLevel',array('option'=>'edit'))) {
			//speziell fuer InteractiveM
			//if (pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_READ)) {
			if (pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_EDIT)) {
				$output->Text(' | ');
				$output->URL($editURL,$editTXT);
				$output->Text(' | ');
				$output->URL($deleteURL,$deleteTXT);
			}
			elseif ($user_id== $adr_user) {
				$output->Text(' | ');
				$output->URL($editURL,$editTXT);
				$output->Text(' | ');
				$output->URL($deleteURL,$deleteTXT);
			}
			else {
				$output->Text(' ');
			}
		}
		$output->TableColEnd();
		$output->TableRowEnd();
		//$i == 1 ? $i = 0 :$i=1;
	}
	$output->Text('</table>');

	// Bottom Navigation
	$output->Linebreak(1);
	$output->Text('<div align="center">');
	$numPages = (($total/$items)+1);
	for($i=1;$i<$numPages;$i++) {
		if ($all==0) {
			$params = array('authid'=>pnSecGenAuthKey(),
							'sortview'=>$sortview,
							'catview'=>$catview,
							'menuprivate'=>$menuprivate,
							'all'=>$all,
							'formSearch'=>$formSearch,
							'total'=>$total,
							'page'=>$i,
							'char'=>$selChar);
		}
		else {
			$params = array('authid'=>pnSecGenAuthKey(),
				'sortview'=>$sortview,
				'catview'=>$catview,
				'menuprivate'=>$menuprivate,
				'all'=>$all,
				'formSearch'=>$formSearch,
				'total'=>$total,
				'page'=>$i);
		}
		$pageURL	= pnModURL(__PNADDRESSBOOK__,'user','viewAll',$params);
		if ($i != 1) { $output->Text(' | '); }
		if ($i == $result->AbsolutePage()) { $output->Text('<b><u>'.$i.'</u></b>'); }
		else { $output->URL($pageURL,$i); }
	}
	if (!pnModGetVar(__PNADDRESSBOOK__, 'hidecopyright')==1) {
		$output->Linebreak(1);
		$output->Text('</div><div align="right">pnAddressBook v2.0 &copy; by <a href="http://www.smiatek.com" target="_blank">smiatek.com</a>');
	}
	$output->Text('</div>');

	// End of Page
	$output->Text(pnAddressBook_themetable('end'));
	$output->Linebreak(1);

	// Return the output that has been generated by this function
    $output->SetInputMode(_PNH_PARSEINPUT);
	return $output->GetOutput();
}

//=========================================================================
//  Main Menu
//=========================================================================
function pnAddressBook_user_menu() {
	global $bgcolor1,$bgcolor2,$bgcolor3,$bgcolor4;

	if (pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_EDIT)) {
		$show_menu_off = false;
	}
	else {
		$m = pnModGetVar(__PNADDRESSBOOK__, 'menu_off');
		switch ($m) {
			case 0:
				$show_menu_off = false;
				break;
			case 1:
				$show_menu_off = true;
				break;
			case 2:
				if (pnUserLoggedIn()) { $show_menu_off = false; }
				else { $show_menu_off = true; }
				break;
			default:
				$show_menu_off = true;
				break;
		}
	}
	if ($show_menu_off) {
		$output = new pnHTML();
		$output->SetInputMode(_PNH_VERBATIMINPUT);
		$output->Text(pnAddressBook_themetable('start'));

		// some design ;)
		$bc1 = $bgcolor1;
		$bc2 = $bgcolor2;
		if ($bgcolor1 == $bgcolor2) {
			if ($bgcolor1 == $bgcolor3) {
				$bc2 = $bgcolor4;
			}
			else {
				$bc2 = $bgcolor3;
			}
		}

		$output->Text('<table width="100%" align="center" cellpadding="5" cellspacing="1" bgcolor="'.$bc2.'">');
		$output->TableRowStart();
		$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
		$output->Title(pnVarPrepHTMLDisplay(pnModGetVar(__PNADDRESSBOOK__, 'abtitle')));
		$output->TableColEnd();
		$output->TableRowEnd();
		$output->Text('</table>');
		$output->Text(pnAddressBook_themetable('end'));

		// Return the output that has been generated by this function
	    return $output->GetOutput();
	}
	else {
		// Get the values
		$formvalues = pnModAPIFunc(__PNADDRESSBOOK__,'user','getMenuValues');
		extract($formvalues);
		$menuvars=array('formcall'=>'insert','authid'=>pnSecGenAuthKey(),'catview'=>$catview,'menuprivate'=>$menuprivate,'all'=>$all,'sortview'=>$sortview,'page'=>$page,'char'=>$char,'total'=>$total);

		$addTXT	= pnVarPrepHTMLDisplay(_pnAB_MENU_ADD);
		$addURL = pnModURL(__PNADDRESSBOOK__,'user','insertedit',$menuvars);
		$searchTXT	=pnVarPrepHTMLDisplay(_pnAB_MENU_SEARCH);
		//$globalTXT	=pnVarPrepHTMLDisplay(_MENU_GLOBAL);

		$output = new pnHTML();
		$output->SetInputMode(_PNH_VERBATIMINPUT);
		$output->Text(pnAddressBook_themetable('start'));

		// some design ;)
		$bc1 = $bgcolor1;
		$bc2 = $bgcolor2;
		if ($bgcolor1 == $bgcolor2) {
			if ($bgcolor1 == $bgcolor3) {
				$bc2 = $bgcolor4;
			}
			else {
				$bc2 = $bgcolor3;
			}
		}

		$output->Text('<table width="100%" align="center" cellpadding="5" cellspacing="1" bgcolor="'.$bc2.'">');
		$output->TableRowStart();
		$output->Text('<td colspan="3" align="center" valign="middle" bgcolor="'.$bc1.'">');
		$output->Title(pnVarPrepHTMLDisplay(pnModGetVar(__PNADDRESSBOOK__, 'abtitle')));
		$output->TableColEnd();
		$output->TableRowEnd();
		$output->TableRowStart();

		// Start Category
		$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
		$output->FormStart(pnModURL(__PNADDRESSBOOK__, 'user', 'viewAll'));
		$output->FormHidden('authid', pnSecGenAuthKey());
		//$output->FormHidden('formSearch', $formSearch);
		$output->FormHidden('sortview', $sortview);
		$output->FormHidden('menuprivate', $menuprivate);
		$output->FormHidden('all',$all);
		$output->FormHidden('total',$total);
		$output->FormHidden('page',$page);
		$output->FormHidden('char',$char);
		$output->TableStart();
		$output->TableRowStart();
		$output->TableColStart(1,'right');
		$output->Text(pnVarPrepHTMLDisplay(_pnAB_CATEGORY).': ');
		$output->TableColEnd();
		$output->TableColStart(1,'left');
		$cats = pnModAPIFunc(__PNADDRESSBOOK__,'user','getCategories');
	    if(!is_array($cats)) {
	        $output->Text($cats);
	        return $output->GetOutput();
	    }
		$args = array('fieldname'=>'catview',
						'data'=>$cats,
						'multiple'=>'0',
						'size'=>'1',
						'selected'=>$catview,
						'accesskey'=>'');
		$output->Text(pnModAPIFunc(__PNADDRESSBOOK__,'user','FormSelect',$args));
		$output->TableColEnd();
		$output->TableRowEnd();
		$output->TableEnd();
		$output->FormEnd();
		$output->TableColEnd();
		// End Category

		// Start Sort By
		$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
		$output->FormStart(pnModURL(__PNADDRESSBOOK__, 'user', 'viewAll'));
		$output->FormHidden('authid', pnSecGenAuthKey());
		$output->FormHidden('catview', $catview);
		//$output->FormHidden('formSearch', $formSearch);
		$output->FormHidden('menuprivate', $menuprivate);
		$output->FormHidden('all',$all);
		$output->FormHidden('total',$total);
		$output->FormHidden('page',$page);
		$output->FormHidden('char',$char);
		$output->TableStart();
		$output->TableRowStart();
		$output->TableColStart(1,'right');
		$output->Text(pnVarPrepHTMLDisplay(_pnAB_SORTBY).': ');
		$output->TableColEnd();
		$output->TableColStart(1,'left');
		$sortby_1 = pnModAPIFunc(__PNADDRESSBOOK__,'user','getSortBy',array('sort'=>1));
		$sortby_2 = pnModAPIFunc(__PNADDRESSBOOK__,'user','getSortBy',array('sort'=>2));
		$sortby[] = array('id'=>0,'name'=>pnVarPrepHTMLDisplay($sortby_1));
		$sortby[] = array('id'=>1,'name'=>pnVarPrepHTMLDisplay($sortby_2));

		$args = array('fieldname'=>'sortview',
						'data'=>$sortby,
						'multiple'=>'0',
						'size'=>'1',
						'selected'=>$sortview,
						'accesskey'=>'');
		$output->Text(pnModAPIFunc(__PNADDRESSBOOK__,'user','FormSelect',$args));
		$output->TableColEnd();
		$output->TableRowEnd();
		$output->TableEnd();
		$output->FormEnd();
		$output->TableColEnd();
		// End Sort By

		// Start Search
		$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
		$output->FormStart(pnModURL(__PNADDRESSBOOK__, 'user', 'viewAll'));
		$output->FormHidden('authid', pnSecGenAuthKey());
		$output->FormHidden('catview', $catview);
		$output->FormHidden('sortview', $sortview);
		$output->FormHidden('menuprivate', $menuprivate);
		$output->FormHidden('all','1');
		$output->TableStart();
		$output->TableRowStart();
		$output->TableColStart(1,'right');
		$output->Text(pnVarPrepHTMLDisplay(_pnAB_MENU_SEARCH).': ');
		$output->TableColEnd();
		$output->TableColStart(1,'left');
		$output->FormText('formSearch', '', 12, 60);
		$output->TableColEnd();
		$output->TableRowEnd();
		$output->TableEnd();
		$output->FormEnd();
		$output->TableColEnd();
		// End Search

		$output->TableRowEnd();

		if ((pnModGetVar(__PNADDRESSBOOK__, 'menu_semi') != 1) || (pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_EDIT))) {
			$output->TableRowStart();

			// Start Private Only
			$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
			$output->TableStart();
			$output->TableRowStart();
			//$output->TableColStart(1,'center');
			$output->Text('<td align="center" valign="middle">');

			if (pnUserLoggedIn()) {
				$privateURL=(pnModURL(__PNADDRESSBOOK__, 'user', 'viewAll'));
				$output->Text('<form name="privateform" method="post" action="'.$privateURL.'">');
				$output->FormHidden('authid', pnSecGenAuthKey());
				$output->FormHidden('catview', $catview);
				//$output->FormHidden('formSearch', $formSearch);
				$output->FormHidden('sortview', $sortview);
				$output->FormHidden('all',$all);
				$output->FormHidden('total',$total);
				$output->FormHidden('page',$page);
				$output->FormHidden('char',$char);
				if (((pnModGetVar(__PNADDRESSBOOK__, 'globalprotect'))==1) && (!pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_EDIT))) {
					$menuprivate == 1;
					$privateTXT = pnVarPrepHTMLDisplay(_pnAB_VIEWPRIVATE_2);
					$output->Text($privateTXT);
					$output->FormEnd();
				}
				else {
					if ($menuprivate == 1) {
						$output->FormHidden('menuprivate',0);
						$privateTXT = pnVarPrepHTMLDisplay(_pnAB_VIEWPRIVATE_2);
					}
					else {
						$output->FormHidden('menuprivate',1);
						$privateTXT = pnVarPrepHTMLDisplay(_pnAB_VIEWPRIVATE_1);
					}
					$output->Text('<a href="#" onclick="document.privateform.submit();">'.$privateTXT.'</a>');
					$output->FormEnd();
				}
			}
//gehSTART - add export records link
				$exportTXT	= pnVarPrepHTMLDisplay(_pnAB_MENU_EXPORT);
				$exportURL = pnModURL(__PNADDRESSBOOK__,'user','export');
				$output->URL($exportURL,$exportTXT);
//gehEND				
			$output->TableColEnd();
			$output->TableRowEnd();
			$output->TableEnd();

			$output->TableColEnd();
			// End PrivateOnly

			// Start A-Z/All
			if ($all==1) {
				$azTXT=(pnVarPrepHTMLDisplay(_pnAB_MENU_AZ_2));
				$allTXT=(pnVarPrepHTMLDisplay(_pnAB_MENU_ALL_2));
			}
			else {
				$azTXT=(pnVarPrepHTMLDisplay(_pnAB_MENU_AZ_1));
				$allTXT=(pnVarPrepHTMLDisplay(_pnAB_MENU_ALL_1));
			}
			$params = array('authid'=>pnSecGenAuthKey(),
								'sortview'=>$sortview,
								'catview'=>$catview,
								'menuprivate'=>$menuprivate,
								'all'=>0,
								'char'=>$char);

			$azURL	= pnModURL(__PNADDRESSBOOK__,'user','viewAll',$params);
			$output->Text('<td align="center" valign="top" bgcolor="'.$bc1.'">');
			$output->TableStart();
			$output->TableRowStart();
			$output->TableColStart(1,'center');
			$output->URL($azURL,$azTXT);
			$output->Text(' | ');
			$params = array('authid'=>pnSecGenAuthKey(),
								'sortview'=>$sortview,
								'catview'=>$catview,
								'menuprivate'=>$menuprivate,
								'all'=>1,
								'total'=>$total,
								'page'=>$page,
								'char'=>$char);

			$allURL	= pnModURL(__PNADDRESSBOOK__,'user','viewAll',$params);
		    $output->URL($allURL,$allTXT);
			$output->TableColEnd();
			$output->TableRowEnd();
			$output->TableEnd();
			$output->TableColEnd();
			// End A-Z/all

			// Start Add new record
			$output->Text('<td align="center" valign="top" bgcolor="'.$bc1.'">');
			$output->TableStart();
			$output->TableRowStart();
			$output->TableColStart(1,'center');
			if(pnModAPIFunc(__PNADDRESSBOOK__,'user','checkAccessLevel',array('option'=>'create'))) {
				$output->URL($addURL,$addTXT);
			}
			$output->TableColEnd();
			$output->TableRowEnd();
			$output->TableEnd();
			$output->TableColEnd();
			// End New Record

			$output->TableRowEnd();
		}

		$output->Text('</table>');
		$output->Text(pnAddressBook_themetable('end'));

	    // Return the output that has been generated by this function
	    return $output->GetOutput();
	}
}

//=========================================================================
//  Open/Close tables like the current theme
//=========================================================================
function pnAddressBook_themetable($option, $type=1) {
	$theme = pnUserGetTheme();
	pnThemeLoad($theme);
    $output = new pnHTML();
	ob_start();
	if ($option == 'start') {
		if ($type == 1) { OpenTable(); }
		else { OpenTable2(); }
	}
	else {
		if ($type == 1) { CloseTable(); }
		else { CloseTable2(); }
	}
	$table = ob_get_contents();
	ob_end_clean();
	$output->SetInputMode(_PNH_VERBATIMINPUT);
	$output->Text($table);

	return $output->GetOutput();
}

//=========================================================================
//  Show detail page
//=========================================================================
function pnAddressBook_user_viewDetail() {
	global $bgcolor1,$bgcolor2,$bgcolor3,$bgcolor4;

	// Get the values
	$values = pnModAPIFunc(__PNADDRESSBOOK__,'user','getsubmitvalues');
	extract($values);

	// Get detailed values from database
	$details = pnModAPIFunc(__PNADDRESSBOOK__,'user','getDetailValues',array('id'=>$id));
	extract($details);

	// Get the labels
   $labels = pnModAPIFunc(__PNADDRESSBOOK__,'user','getLabels');
    if(!is_array($labels)) {
        $output->Text($labels);
        return $output->GetOutput();
    }

	$output = new pnHTML();
	$output->SetInputMode(_PNH_VERBATIMINPUT);
	$output->Text(pnAddressBook_themetable('start'));

	// some design ;)
	$bc1 = $bgcolor1;
	$bc2 = $bgcolor2;
	if ($bgcolor1 == $bgcolor2) {
		if ($bgcolor1 == $bgcolor3) {
			$bc2 = $bgcolor4;
		}
		else {
			$bc2 = $bgcolor3;
		}
	}

	$output->Text('<table width="100%" align="center" cellpadding="5" cellspacing="1" bgcolor="'.$bc2.'">');
	$output->TableRowStart();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->Title(pnVarPrepHTMLDisplay(pnModGetVar(__PNADDRESSBOOK__, 'abtitle')));
	$output->TableColEnd();
	$output->TableRowEnd();
	$output->Text('</table>');
	$output->Text(pnAddressBook_themetable('end'));


	$output->Text(pnAddressBook_themetable('start'));

	// General information
	// headline
	$cats = pnModAPIFunc(__PNADDRESSBOOK__,'user','getFormCategories');
	$info = pnVarPrepHTMLDisplay(_pnAB_CATEGORY.': '._pnAB_UNFILED);
	if ($cat_id > 0) {
		foreach ($cats as $c) {
			if ($cat_id == $c['nr']) {
				$info = pnVarPrepHTMLDisplay(_pnAB_CATEGORY.': '.$c['name']);
			}
		}
	}
	//$cat_id > 0 ? $cat = $cats[$cat_id-1]['name'] : $cat = pnVarPrepHTMLDisplay(_pnAB_UNFILED);
	//$info = pnVarPrepHTMLDisplay(_pnAB_CATEGORY.': '.$cat);
	if ($date > 0) {
		$info .= ' | '.pnVarPrepHTMLDisplay(_pnAB_LASTCHANGED).ml_ftime(_DATETIMEBRIEF, $date);
	}
	$output->Text('<table width="100%" border="0" align="center" cellpadding="5" cellspacing="1"><tr><td align="left">'.$info.'</td></tr></table>');
	// outer table
	$output->Text('<table width="100%" align="center" cellpadding="5" cellspacing="1" bgcolor="'.$bc2.'">');
	$output->TableRowStart();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	// inner table left column: name/address
	$output->Text('<table border="0" cellpadding="0" cellspacing="0" align="left">');
	$output->TableRowStart();
	$output->TableColStart(1,'left','top');
	// Version fuer Arno
	//$prfx = pnModAPIFunc(__PNADDRESSBOOK__,'user','getPrefix',array('id'=>$prfx));
	if (!empty($lname) || !empty($fname)) {
		if (!empty($fname)) {
			$output->Text('<b>'.pnVarPrepHTMLDisplay($fname).' '.pnVarPrepHTMLDisplay($lname).'</b>');
			// Version fuer Arno
			//$output->Text('<b>'.pnVarPrepHTMLDisplay($prfx).' '.pnVarPrepHTMLDisplay($fname).' '.pnVarPrepHTMLDisplay($lname).'</b>');
			$output->Linebreak(2);
			if (!empty($company)) {
				$output->Text(pnVarPrepHTMLDisplay($company));
				$output->Linebreak(1);
			}
			if (!empty($title)) {
				$output->Text(pnVarPrepHTMLDisplay($title));
				$output->Linebreak(1);
			}
		}
		else {
			$output->Text('<b>'.pnVarPrepHTMLDisplay($lname).'</b>');
			$output->Linebreak(2);
			if (!empty($company)) {
				$output->Text(pnVarPrepHTMLDisplay($company));
				$output->Linebreak(1);
			}
			if (!empty($title)) {
				$output->Text(pnVarPrepHTMLDisplay($title));
				$output->Linebreak(1);
			}
		}
	}
	else {
		$output->Text('<b>'.pnVarPrepHTMLDisplay($company).'</b>');
		$output->Linebreak(2);
		if (!empty($title)) {
			$output->Text(pnVarPrepHTMLDisplay($title));
			$output->Linebreak(1);
		}
	}
	$output->Linebreak(1);
	if (!empty($address1)) {
		$output->Text(pnVarPrepHTMLDisplay($address1));
		$output->Linebreak(1);
	}
	if (!empty($address2)) {
		$output->Text(pnVarPrepHTMLDisplay($address2));
		$output->Linebreak(1);
	}
	$zipbeforecity = pnModGetVar(__PNADDRESSBOOK__,'zipbeforecity');
	if ($zipbeforecity) {
		if (!empty($zip)) {
			$output->Text(pnVarPrepHTMLDisplay($zip));
			$output->Text(' ');
		}
		if (!empty($city)) {
			$output->Text(pnVarPrepHTMLDisplay($city));
			$output->Linebreak(1);
		}
		if (!empty($state)) {
			$output->Text(pnVarPrepHTMLDisplay($state));
			$output->Linebreak(1);
		}
		if (!empty($country)) {
			$output->Text(pnVarPrepHTMLDisplay($country));
			$output->Linebreak(1);
		}
	}
	else {
		if (!empty($city)) {
			$output->Text(pnVarPrepHTMLDisplay($city));
			$output->Linebreak(1);
		}
		if (!empty($state)) {
			$output->Text(pnVarPrepHTMLDisplay($state));
			//$output->Linebreak(1);
		}
		if (!empty($zip)) {
			$output->Text(pnVarPrepHTMLDisplay(' '.$zip));
			$output->Linebreak(1);
		}
		if (!empty($country)) {
			$output->Text(pnVarPrepHTMLDisplay($country));
			$output->Linebreak(1);
		}
	}
	$output->TableColEnd();
	// inner table middle column: space
	//$output->Text('<td width="50"></td>');
	$output->Text('<td width="24"></td><td width="1" bgcolor="'.$bc2.'"><img width="1" height="1" src="modules/pnAddressBook/pnimages/pix.gif" alt=""></td><td width="24"></td>');
	// inner table right column: contact info
	$output->Text('<td align="left" valign="top">');
	$output->Text(pnVarPrepHTMLDisplay('<b>'._pnAB_CONTACT.':</b>'));
	$output->Linebreak(2);
	$output->Text('<table border="0" cellpadding="0" cellspacing="0" align="left">');
	for ($i=1;$i<6;$i++) {
		$the_contact = 'contact_'.$i;
		$the_label = 'c_label_'.$i;
		if (!empty($$the_contact)) {
			foreach ($labels as $lab) {
				if ($$the_label == $lab['nr']) {
					$output->TableRowStart();
					$output->TableColStart(1,'left','top');
					$output->Text(pnVarPrepHTMLDisplay($lab['name'].': '));
					$output->TableColEnd();
					$output->Text('<td width="10"></td>');
					$output->TableColStart(1,'left');
					if(!pnModAPIFunc(__PNADDRESSBOOK__,'user','is_email',array('email'=>$$the_contact))) {
						if(!pnModAPIFunc(__PNADDRESSBOOK__,'user','is_url',array('url'=>$$the_contact))) {
							$output->Text(pnVarPrepHTMLDisplay($$the_contact));
						}
						else {
							$output->Text('<a href="'.pnVarPrepHTMLDisplay($$the_contact).'" target="_blank">'.pnVarPrepHTMLDisplay($$the_contact).'</a>');
						}
					}
					else {
						$output->Text('<a href="mailto:'.pnVarPrepHTMLDisplay($$the_contact).'">'.pnVarPrepHTMLDisplay($$the_contact).'</a>');
					}
					$output->TableColEnd();
					$output->TableRowEnd();
				}
			}
		}
	}
	$output->Text('</table>');
	$output->TableColEnd();

	// image??
    if ((!empty($img))&&($img!='NULL')) {
		// inner table middle column: space
		$output->Text('<td width="24"></td><td width="1" bgcolor="'.$bc2.'"><img width="1" height="1" src="modules/pnAddressBook/pnimages/pix.gif" alt=""></td><td width="24"></td>');
		// inner table right column: contact info
		$output->Text('<td align="right" valign="middle">');
		$output->Text('<img src="modules/pnAddressBook/img/'.$img.'" border="0" alt="">');
		$output->TableColEnd();
	}
	$output->TableRowEnd();
	$output->Text('</table>');
	// end inner table
	$output->TableColEnd();
	$output->TableRowEnd();
	$output->Text('</table>');
	// outer table

	// Custom information
	$custom_tab = pnModGetVar(__PNADDRESSBOOK__,'custom_tab');
	if ((!empty($custom_tab)) || ($custom_tab != '')) {
		$cus_fields = pnModAPIFunc(__PNADDRESSBOOK__,'user','customFieldInformation',array('id'=>$id));
		$hasValues = false;
        foreach($cus_fields as $cus) {
          if ((!empty($cus['value'])) && ($cus['value'] != '')) {
                $hasValues = true;
				break;
			}
		}
		if ((!strstr($cus['type'],'tinyint')) && (!strstr($cus['type'],'smallint'))) {
			$hasValues = true;
		}
		if ($hasValues) {
			$output->Linebreak(1);
			$output->Text('<table width="100%" border="0" align="center" cellpadding="5" cellspacing="1"><tr><td align="left">'.pnVarPrepHTMLDisplay(pnModGetVar(__PNADDRESSBOOK__,'custom_tab')).'</td></tr></table>');
			// outer table
			$output->Text('<table width="100%" align="center" cellpadding="5" cellspacing="1" bgcolor="'.$bc2.'">');
			$output->TableRowStart();
			$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
			// inner table left column: name/address
			$output->Text('<table border="0" cellpadding="0" cellspacing="0" align="left">');
			$output->TableRowStart();
			// inner table column: custom info
			$output->Text('<td align="left" valign="top">');
			//$output->Linebreak(2);
			$output->Text('<table border="0" cellpadding="0" cellspacing="0" align="left">');

			foreach($cus_fields as $cus) {
				if ($cus['type']=='date default NULL') {
						$cus['value'] = pnModAPIFunc(__PNADDRESSBOOK__,'user','stamp2date',array('idate'=>$cus['value']));
					}
				if ($cus['type']=='decimal(10,2) default NULL') {
						$cus['value'] = pnModAPIFunc(__PNADDRESSBOOK__,'user','num4display',array('inum'=>$cus['value']));
				}
				if ((strstr($cus['type'],'tinyint')) || (strstr($cus['type'],'smallint'))) {
					if (strstr($cus['type'],'tinyint')) {
						$output->TableRowStart();
						$output->TableColStart(3,'left','top');
						$output->Text('<br>');
						$output->TableColEnd();
						$output->TableRowEnd();
					}
					else {
						$output->TableRowStart();
						$output->TableColStart(3,'left','top');
						$output->Text('<hr>');
						$output->TableColEnd();
						$output->TableRowEnd();
					}
				}
				else {
//gehSTART
                    if ($cus['type']=='int(1) default NULL') {                              //gehINSERT
                        ($cus['value'] == 1) ? $cus['value'] = "Yes" : $cus['value'] = "";  //gehINSERT
                    }
//gehEND
					if (!empty($cus['value'])) {
						$output->TableRowStart();
						$output->TableColStart(1,'left','top');
						$output->Text(pnVarPrepHTMLDisplay($cus['name']).': ');
						$output->TableColEnd();
						$output->Text('<td width="10"></td>');
						$output->TableColStart(1,'left','top');
						$output->Text(pnVarPrepHTMLDisplay(nl2br($cus['value'])));
						// $output->Linebreak(2);
						$output->TableColEnd();
						$output->TableRowEnd();
					}
				}
			}
			$output->Text('</table>');
			$output->TableColEnd();
			$output->TableRowEnd();
			$output->Text('</table>');
			// end inner table
			$output->TableColEnd();
			$output->TableRowEnd();
			$output->Text('</table>');
			// outer table
		}
	}

	// Notes
	if (!empty($note)) {
		// headline
		$output->Linebreak(1);
		$output->Text('<table width="100%" border="0" align="center" cellpadding="5" cellspacing="1"><tr><td align="left">'.pnVarPrepHTMLDisplay(_pnAB_NOTETAB).'</td></tr></table>');
		// outer table
		$output->Text('<table width="100%" align="center" cellpadding="5" cellspacing="1" bgcolor="'.$bc2.'">');
		$output->TableRowStart();
		$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
		// inner table left column: name/address
		$output->Text('<table border="0" cellpadding="0" cellspacing="0" align="left">');
		$output->TableRowStart();
		// inner table column: custom info
		$output->Text('<td align="left" valign="top">');
		//$output->Linebreak(2);
		$output->Text(pnVarPrepHTMLDisplay(nl2br($note)));
		$output->TableColEnd();
		$output->TableRowEnd();
		$output->Text('</table>');
		// end inner table
		$output->TableColEnd();
		$output->TableRowEnd();
		$output->Text('</table>');
		// outer table
	}

	$output->Text(pnAddressBook_themetable('end'));

	// Go back
	$output->Text('<form>');
	$output->Text(pnAddressBook_themetable('start'));
	$output->Text('<div align="center"><br>');
	// Copy to clipboard if IE
	if (pnModAPIFunc(__PNADDRESSBOOK__,'user','checkForIE')) {
		$clip='';
		if (!empty($company)) {$clip.=$company.'\n'; }
		if (!empty($lname)) {
			if (!empty($fname)) {$clip.=$fname.' '.$lname.'\n'; }
			else { $clip .= $lname.'\n'; }
		}
		if (!empty($address1)) {$clip.=$address1.'\n'; }
		if (!empty($address2)) {$clip.=$address2.'\n'; }
		if ($zipbeforecity) {
			if (!empty($zip)) {$clip.=$zip.' '; }
			if (!empty($city)) {$clip.=$city.'\n'; }
			if (!empty($state)) {$clip.=$state.'\n'; }
			if (!empty($country)) {$clip.=$country.'\n'; }
		}
		else {
			if (!empty($city)) {$clip.=$city.'\n'; }
			if (!empty($state)) {$clip.=$state.'\n'; }
			if (!empty($zip)) {$clip.=$zip.'\n'; }
			if (!empty($country)) {$clip.=$country.'\n'; }
		}
		$output->Text('<input type="button" value="'.pnVarPrepHTMLDisplay(_pnAB_COPY).'" onclick="javascript:window.clipboardData.setData(\'Text\',\''.$clip.'\');">&nbsp;&nbsp;&nbsp;');
	}
	$output->Text('<input type="button" value="'.pnVarPrepHTMLDisplay(_pnAB_GOBACK).'" onclick="javascript:history.go(-1);">');
	$output->Text('<br><br></div>');
	$output->Text(pnAddressBook_themetable('end'));
    $output->FormEnd();

	return $output->GetOutput();

}

//=========================================================================
//  Confirm deletion
//=========================================================================
function pnAddressBook_user_confirmDelete() {
	global $bgcolor1,$bgcolor2,$bgcolor3,$bgcolor4;

	// save menu settings
	$menuvalues = pnModAPIFunc(__PNADDRESSBOOK__,'user','getMenuValues');
	extract($menuvalues);
	$menuvars=array('catview'=>$catview,'menuprivate'=>$menuprivate,'all'=>$all,'sortview'=>$sortview,'page'=>$page,'char'=>$char,'total'=>$total);

	// Get the values
	$values = pnModAPIFunc(__PNADDRESSBOOK__,'user','getsubmitvalues');
	extract($values);
	// Get detailed values from database
	$details = pnModAPIFunc(__PNADDRESSBOOK__,'user','getDetailValues',array('id'=>$id));
	extract($details);

	$output = new pnHTML();
	$output->SetInputMode(_PNH_VERBATIMINPUT);
	$output->Text(pnAddressBook_themetable('start'));

	// some design ;)
	$bc1 = $bgcolor1;
	$bc2 = $bgcolor2;
	if ($bgcolor1 == $bgcolor2) {
		if ($bgcolor1 == $bgcolor3) {
			$bc2 = $bgcolor4;
		}
		else {
			$bc2 = $bgcolor3;
		}
	}

	$output->Text('<table width="100%" align="center" cellpadding="5" cellspacing="1" bgcolor="'.$bc2.'">');
	$output->TableRowStart();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->Title(pnVarPrepHTMLDisplay(pnModGetVar(__PNADDRESSBOOK__, 'abtitle')));
	$output->TableColEnd();
	$output->TableRowEnd();
	$output->Text('</table>');
	$output->Text(pnAddressBook_themetable('end'));

	$output->Linebreak(1);
	$output->Text(pnAddressBook_themetable('start'));
	$output->Linebreak(1);
	$output->Text('<div align="center">');
	$output->Text(pnVarPrepHTMLDisplay('<b>'._pnAB_CONFIRMDELETE.'</b>'));
	$output->Linebreak(2);
	if (!empty($lname)) {
		if (!empty($fname)) {
			$output->Text(pnVarPrepHTMLDisplay($fname).' '.pnVarPrepHTMLDisplay($lname));
			$output->Linebreak(1);
			if (!empty($company)) {
				$output->Text(pnVarPrepHTMLDisplay($company));
				$output->Linebreak(1);
			}
		}
		else {
			$output->Text(pnVarPrepHTMLDisplay($lname));
			$output->Linebreak(1);
			if (!empty($company)) {
				$output->Text(pnVarPrepHTMLDisplay($company));
				$output->Linebreak(1);
			}
		}
	}
	else {
		$output->Text(pnVarPrepHTMLDisplay($company));
		$output->Linebreak(1);
	}
	$output->Text('</div>');
	$output->Linebreak(1);
	$output->Text(pnAddressBook_themetable('end'));

	// Go back or delete
	$output->FormStart(pnModURL(__PNADDRESSBOOK__, 'user', 'doDelete',$menuvars));
	$output->FormHidden('authid', pnSecGenAuthKey());
	$output->FormHidden('id', $id);
	$output->Linebreak(1);
	$output->Text(pnAddressBook_themetable('start'));
	$output->Text('<div align="center"><br>');
	$output->Text('<input type="submit" value="'.pnVarPrepHTMLDisplay(_pnAB_DELETE).'">&nbsp;&nbsp;&nbsp;');
	$output->Text('<input type="button" value="'.pnVarPrepHTMLDisplay(_pnAB_CANCEL).'" onclick="javascript:history.go(-1);"');
	$output->Text('<br><br></div>');
	$output->Text(pnAddressBook_themetable('end'));
    $output->FormEnd();

	return $output->GetOutput();
}

//=========================================================================
//  Delete a record
//=========================================================================
function pnAddressBook_user_doDelete() {

	// Security check
	// Confirm authorisation code
	//if (!pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_READ)) {
	if (!pnSecConfirmAuthKey()) {
	    $output = new pnHTML();
		$output->SetInputMode(_PNH_VERBATIMINPUT);
		$output->Text(pnAddressBook_themetable('start'));
		$output->Text(pnVarPrepHTMLDisplay(_PNADDRESSBOOK_NOAUTH));
        $output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
    }
	$id = pnVarCleanFromInput('id');

	if(!pnModAPIFunc(__PNADDRESSBOOK__,'user','deleterecord',array('id'=>$id))) {
        $output = new pnHTML();
		$output->SetInputMode(_PNH_VERBATIMINPUT);
	    $output->Text(pnAddressBook_themetable('start'));
		$output->Text(pnVarPrepHTMLDisplay(_pnAB_DELETENOSUCCESS));
		$output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
    }

	// save menu settings
	$menuvalues = pnModAPIFunc(__PNADDRESSBOOK__,'user','getMenuValues');
	extract($menuvalues);
	$menuvars=array('catview'=>$catview,'menuprivate'=>$menuprivate,'all'=>$all,'sortview'=>$sortview,'page'=>$page,'char'=>$char,'total'=>$total);

	// This function generated no output
    pnRedirect(pnModURL(__PNADDRESSBOOK__, 'user', 'viewAll',$menuvars));

    // Return
    return true;
}

//=========================================================================
//  Insert/Edit form
//=========================================================================
function pnAddressBook_user_insertEdit() {
	global $bgcolor1,$bgcolor2,$bgcolor3,$bgcolor4;

	 //Security Check
	 if (!pnSecConfirmAuthKey()) {
		$output = new pnHTML();
		$output->SetInputMode(_PNH_VERBATIMINPUT);
		$output->Text(pnAddressBook_themetable('start'));
		$output->Text(pnVarPrepHTMLDisplay(_PNADDRESSBOOK_NOAUTH));
        $output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
    }

	// save menu settings
	$menuvalues = pnModAPIFunc(__PNADDRESSBOOK__,'user','getMenuValues');
	extract($menuvalues);

	$output = new pnHTML();
	$output->SetInputMode(_PNH_VERBATIMINPUT);
	$output->Linebreak(1);
	$output->Text(pnAddressBook_themetable('start'));

	// some design ;)
	$bc1 = $bgcolor1;
	$bc2 = $bgcolor2;
	if ($bgcolor1 == $bgcolor2) {
		if ($bgcolor1 == $bgcolor3) {
			$bc2 = $bgcolor4;
		}
		else {
			$bc2 = $bgcolor3;
		}
	}

	$output->Text('<table width="100%" align="center" cellpadding="5" cellspacing="1" bgcolor="'.$bc2.'">');
	$output->TableRowStart();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->Title(pnVarPrepHTMLDisplay(pnModGetVar(__PNADDRESSBOOK__, 'abtitle')));
	$output->TableColEnd();
	$output->TableRowEnd();
	$output->Text('</table>');
	$output->Text(pnAddressBook_themetable('end'));
//gehSTART
//	$output->Linebreak(1); //gehREMOVED
//gehEND
	// Insert or Edit
	$formcall = pnVarCleanFromInput('formcall');

	// Get the values
	$formvalues = pnModAPIFunc(__PNADDRESSBOOK__,'user','getsubmitvalues');
	extract($formvalues);
	$action = pnVarCleanFromInput('action');
		if (!isset($action)) {
		// Get detailed values from database
		$details = pnModAPIFunc(__PNADDRESSBOOK__,'user','getDetailValues',array('id'=>$id));
		extract($details);
		$action = 0;
	}

	$menuvars=array('catview'=>$catview,'menuprivate'=>$menuprivate,'all'=>$all,'sortview'=>$sortview,'page'=>$page,'char'=>$char,'total'=>$total);
	// Cancelled??
	if (pnVarCleanFromInput('cancel')) {
		pnRedirect(pnModURL(__PNADDRESSBOOK__, 'user', 'viewAll',$menuvars));
		return true;
	}

	// Update button clicked?
	if (pnVarCleanFromInput('update')) {
		$check1=pnModAPIFunc(__PNADDRESSBOOK__,'user','checksubmitvalues',$formvalues);
		if ($check1) {
			$output->Title(pnVarPrepHTMLDisplay($check1));
		}
		else {
			//
			$check2=pnModAPIFunc(__PNADDRESSBOOK__,'user','updaterecord',$formvalues);
			if (!$check2) {
				 $output->Title(pnVarPrepHTMLDisplay('<b>'._pnAB_UPDATE_ERROR.'</b>'));
				 return $output->GetOutput();
			}
			else {
				$menuvalues = pnModAPIFunc(__PNADDRESSBOOK__,'user','getMenuValues');
				pnRedirect(pnModURL(__PNADDRESSBOOK__, 'user', 'viewAll',$menuvars));
				return true;
			}
		}
	}

	// Insert button clicked?
	if (pnVarCleanFromInput('insert')) {
		$check1=pnModAPIFunc(__PNADDRESSBOOK__,'user','checksubmitvalues',$formvalues);
		if ($check1) {
			$output->Title(pnVarPrepHTMLDisplay($check1));
		}
		else {
			//
			$check2=pnModAPIFunc(__PNADDRESSBOOK__,'user','insertrecord',$formvalues);
			if (!$check2) {
				 $output->Title(pnVarPrepHTMLDisplay('<b>'._pnAB_INSERT_ERROR.'</b>'));
				 return $output->GetOutput();
			}
			else {
				$menuvars=array('formcall'=>'insert','authid'=>pnSecGenAuthKey(),'catview'=>$catview,'menuprivate'=>$menuprivate,'all'=>$all,'sortview'=>$sortview,'page'=>$page,'char'=>$char,'total'=>$total);
				$output->Text(pnAddressBook_themetable('start'));
				$output->Linebreak(1);
				$output->Text('<div align="center">');
				$output->Text(pnVarPrepHTMLDisplay('<b>'._pnAB_INSERT_pnAB_SUCCESS.'</b>'));
				$addTXT	= pnVarPrepHTMLDisplay(_pnAB_MENU_ADD);
				$addURL = pnModURL(__PNADDRESSBOOK__,'user','insertedit',$menuvars);
				$backTXT = pnVarPrepHTMLDisplay('['._pnAB_GOBACK.']');
				$backURL = pnModURL(__PNADDRESSBOOK__,'user','viewAll',$menuvars);
				$output->Linebreak(2);
				if(pnModAPIFunc(__PNADDRESSBOOK__,'user','checkAccessLevel',array('option'=>'create'))) {
					$output->URL($addURL,$addTXT);
					$output->Text('&nbsp;&nbsp;&nbsp;');
				}
				$output->URL($backURL,$backTXT);
				$output->Text('</div>');
				$output->Linebreak(1);
				$output->Text(pnAddressBook_themetable('end'));
				return $output->GetOutput();
			}
		}
	}


	// Get user id
	if (pnUserLoggedIn()) {
		$user_id = pnUserGetVar('uid');
	}

	// Start form
   	//$output->Text('<form name="addcontact" action="'.pnModURL(__PNADDRESSBOOK__, 'user', 'insertedit').'" method="post" enctype="application/x-www-form-urlencoded"');
	$output->Text('<form name="addcontact" action="'.pnModURL(__PNADDRESSBOOK__, 'user', 'insertedit').'" method="post" enctype="application/x-www-form-urlencoded">');
	//$output->Text('<form name="addcontact" action="'.pnModURL(__PNADDRESSBOOK__, 'user', 'insertedit').'" method="post">');

	// Add an authorisation ID
    $output->FormHidden('authid', pnSecGenAuthKey());

	// Add registerlabelclicked
	$output->FormHidden('action', $action);
	$output->Text(pnAddressBook_themetable('start'));
//gehSTART
    $output->Title($fname." ".$lname);  //gehINSERT
//  $output->Linebreak(1);              //gehREMOVED
//gehEND

	// tab selection
	switch($action) {
		case 0:
			include('modules/'.__PNADDRESSBOOK__.'/inc/user_form_tab_0.inc');
			break;
		case 1:
			include('modules/'.__PNADDRESSBOOK__.'/inc/user_form_tab_1.inc');
			break;
		case 2:
			include('modules/'.__PNADDRESSBOOK__.'/inc/user_form_tab_2.inc');
			break;
		case 3:
			include('modules/'.__PNADDRESSBOOK__.'/inc/user_form_tab_3.inc');
			break;
		case 4:
			include('modules/'.__PNADDRESSBOOK__.'/inc/user_form_tab_4.inc');
			break;
		case 5:
			include('modules/'.__PNADDRESSBOOK__.'/inc/user_form_tab_5.inc');
			break;
		default:
			include('modules/'.__PNADDRESSBOOK__.'/inc/user_form_tab_0.inc');
			break;
	}

	// End form
//	$output->Linebreak(1);                              //gehREMOVE
	$output->Text(pnAddressBook_themetable('start'));
//gehSTART
//    $output->Text('<div align="center"><br>');        //gehREMOVED
    $output->Text('<div align="center">');              //gehINSERT
//gehEND
	if ($formcall == 'edit') {
		$output->Text('<input type="submit" name="update" value="'.pnVarPrepHTMLDisplay(_pnAB_UPDATE_RECORD).'">&nbsp;&nbsp;&nbsp;');
	}
	else {
		$output->Text('<input type="submit" name="insert" value="'.pnVarPrepHTMLDisplay(_pnAB_INSERT_RECORD).'">&nbsp;&nbsp;&nbsp;');
	}
	$output->Text('<input type="submit" name="cancel" value="'.pnVarPrepHTMLDisplay(_pnAB_CANCEL).'">&nbsp;&nbsp;&nbsp;');
	$output->Text('<br><br></div>');
	$output->Text(pnAddressBook_themetable('end'));
    $output->FormEnd();

	return $output->GetOutput();
}

//gehSTART
//=========================================================================
//  Export the entire list
//=========================================================================
function pnAddressBook_user_export() {

	// SQL Query
	list($dbconn) = pnDBGetConn();
    $pntable = pnDBGetTables();
	$address_table = $pntable['pnaddressbook_address'];
    $address_column = &$pntable['pnaddressbook_address_column'];

	$sql = "SELECT " . $address_column['prefix'] .",
				   " . $address_column['lname'] .",
				   " . $address_column['fname'] .",
				   " . $address_column['title'] .",
				   " . $address_column['company'] .",
				   " . $address_column['zip'] .",
				   " . $address_column['city'] .",
				   " . $address_column['address1'] .",
				   " . $address_column['address2'] .",
				   " . $address_column['state'] .",
				   " . $address_column['country'] .",
				   " . $address_column['contact_1'] .",
				   " . $address_column['contact_2'] .",
				   " . $address_column['contact_3'] .",
				   " . $address_column['contact_4'] .",
				   " . $address_column['contact_5'] .",
				   " . $address_column['custom_1'] .",
				   " . $address_column['custom_2'] .",
				   " . $address_column['custom_3'] .",
				   " . $address_column['custom_4'] .",
				     	adr_custom_5,
				     	adr_custom_6,
				     	adr_custom_7,
				     	adr_custom_8,
				     	adr_custom_9,
					adr_custom_10,
					adr_custom_11,
					adr_custom_12,
					adr_custom_13,
					adr_custom_14,
					adr_custom_17,
					adr_custom_18,
					adr_custom_19,
					adr_custom_20,
					adr_custom_21,
					adr_custom_22,
					adr_custom_23,

					adr_custom_15,
					adr_custom_16,

				   " . $address_column['note'] . ",
				   " . $address_column['date'];

	$sql .= " FROM " . $address_table . " ORDER BY " . $address_column['lname'] . "";

	$result = $dbconn->Execute($sql);

	if ($dbconn->ErrorNo() != 0) {
		global $bgcolor1,$bgcolor2,$bgcolor3,$bgcolor4;
	
		$output = new pnHTML();
		$output->SetInputMode(_PNH_VERBATIMINPUT);
		$output->Text(pnAddressBook_themetable('start'));
        	$output->Text('ERROR ' . $dbconn->ErrorNo() . "<br>" . $sql);
		$output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
    }

// List Header
	$address_export  = '"Prefix","First Name","Last Name","Title","Company","Address 1","Address 2","City","State","Zipcode","Country",';
	$address_export .= '"Contact 1","Contact 2","Contact 3","Contact 4","Contact 5",';
//geh Custom fields
	$address_export .= '"Donor",'; 			// adr_custom_1
	$address_export .= '"Caretaker",'; 		// adr_custom_2
	$address_export .= '"Member",';			// adr_custom_3		
	$address_export .= '"TNR",'; 			// adr_custom_4
	$address_export .= '"Volunteer",'; 		// adr_custom_5
	$address_export .= '"Foster",';			// adr_custom_6
	$address_export .= '"Feeder",';			// adr_custom_7
	$address_export .= '"Food Source",';	// adr_custom_8
	$address_export .= '"Packet",';			// adr_custom_9
	$address_export .= '"Contact Date",';	// adr_custom_10
	$address_export .= '"2002 Donations",';	// adr_custom_11
	$address_export .= '"2003 Donations",';	// adr_custom_12
	$address_export .= '"2004 Donations",';	// adr_custom_13
	$address_export .= '"2005 Donations",';	// adr_custom_14
	$address_export .= '"2006 Donations",';	// adr_custom_17
	$address_export .= '"2007 Donations",';	// adr_custom_18
	$address_export .= '"2008 Donations",';	// adr_custom_19
	$address_export .= '"2009 Donations",';	// adr_custom_20
	$address_export .= '"2010 Donations",';	// adr_custom_21
	$address_export .= '"2011 Donations",';	// adr_custom_22
	$address_export .= '"2012 Donations",';	// adr_custom_23

	$address_export .= '"Referred By",';	// adr_custom_15
	$address_export .= '"Graduate",';		// adr_custom_16

	$address_export .= '"Note",';			// note
	$address_export .= '"Last Updated"';	// date
	$address_export .= '"\n"';

	for (; !$result->EOF; $result->MoveNext()) {
		list($prefix
		    ,$lname
			,$fname
			,$title
			,$company
			,$zip
			,$city
			,$address1
			,$address2
			,$state
			,$country
			,$contact_1
			,$contact_2
			,$contact_3
			,$contact_4
			,$contact_5
			,$custom_1
			,$custom_2
			,$custom_3
			,$custom_4
			,$custom_5
			,$custom_6
			,$custom_7
			,$custom_8
			,$custom_9
			,$custom_10
			,$custom_11
			,$custom_12
			,$custom_13
			,$custom_14
			,$custom_17
			,$custom_18
			,$custom_19
			,$custom_20
			,$custom_21
			,$custom_22
			,$custom_23

			,$custom_15		// Referred By
			,$custom_16		// Graduate

			,$note
			,$lastupdate) = $result->fields;


		switch ($prefix) {
			case 1: $address_export .= '"Mr.",'; break;
			case 2: $address_export .= '"Mrs.",'; break;
			case 3: $address_export .= '"Dr.",'; break;
			case 4: $address_export .= '"Ms.",'; break;
			default: $address_export .= '"",'; break;		
		}
		
		$address_export .= '"'.$fname.'",';
		$address_export .= '"'.$lname.'",';
		$address_export .= '"'.$title.'",';
		$address_export .= '"'.$company.'",';
		$address_export .= '"'.$address1.'",';
		$address_export .= '"'.$address2.'",';
		$address_export .= '"'.$city.'",';
		$address_export .= '"'.$state.'",';
		$address_export .= '"'.$zip.'",';
		$address_export .= '"'.$country.'",';
		$address_export .= '"'.$contact_1.'",';
		$address_export .= '"'.$contact_2.'",';
		$address_export .= '"'.$contact_3.'",';
		$address_export .= '"'.$contact_4.'",';
		$address_export .= '"'.$contact_5.'",';
		if (!empty($custom_1)) {$address_export .='"Y",'; } else {$address_export .='"",'; }
		if (!empty($custom_2)) {$address_export .='"Y",'; } else {$address_export .='"",'; }
		if (!empty($custom_3)) {$address_export .='"Y",'; } else {$address_export .='"",'; }
		if (!empty($custom_4)) {$address_export .='"Y",'; } else {$address_export .='"",'; }
		if (!empty($custom_5)) {$address_export .='"Y",'; } else {$address_export .='"",'; }
		if (!empty($custom_6)) {$address_export .='"Y",'; } else {$address_export .='"",'; }
		if (!empty($custom_7)) {$address_export .='"Y",'; } else {$address_export .='"",'; }
		if (!empty($custom_8)) {$address_export .='"Y",'; } else {$address_export .='"",'; }
		if (!empty($custom_9)) {$address_export .='"Y",'; } else {$address_export .='"",'; }

		$address_export .= '"'.date($custom_10).'",';
		$address_export .= '"'.$custom_11.'",';
		$address_export .= '"'.$custom_12.'",';
		$address_export .= '"'.$custom_13.'",';
		$address_export .= '"'.$custom_14.'",';
		$address_export .= '"'.$custom_17.'",';
		$address_export .= '"'.$custom_18.'",';
		$address_export .= '"'.$custom_19.'",';
		$address_export .= '"'.$custom_20.'",';
		$address_export .= '"'.$custom_21.'",';
		$address_export .= '"'.$custom_22.'",';
		$address_export .= '"'.$custom_23.'",';

		$address_export .= '"'.$custom_15.'",';
		$address_export .= '"'.$custom_16.'",';

		$address_export .= '"'.$note.'",';
		$address_export .= '"'.date("m-d-Y",$lastupdate).'"';
		$address_export .="\n";
	}

	header("Content-type: ext/x-csv");
	header('Content-Disposition: filename="FCCC_AddressList_'.date("Y-m-d_h-i-s").'.csv"');
	header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
	print $address_export;

	return TRUE;
}
//gehEND

?>
